package com.itjisu.hongbao.core;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mobads.SplashAd;
import com.baidu.mobads.SplashAdListener;
import com.itjisu.hongbao.R;

public class SplashActivity extends Activity {
    Handler mainHandler = new Handler();
//    AdvancedApiListener advancedApiListener;

    TextView timed;
    int time = 5;
    boolean isClick = false;
    public static boolean splashOK = false;
    Runnable closeRunnable = new Runnable() {
        @Override
        public void run() {
            time--;
            timed.setText(time + "s");
            if (!isClick) {
                if (time <= 0) {
                    jump();
                } else {
                    mainHandler.postDelayed(this, 1000);
                }
            }
        }
    };

    private void goMainActivity() {
        Intent intent = new Intent(SplashActivity.this, QiangHongBaoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
        splashOK = true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        AdView.preLoad(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        final ImageView imageView = (ImageView) findViewById(R.id.splash_img);
        RelativeLayout adsParent= findViewById(R.id.adsRl);
        SplashAdListener listener = new SplashAdListener() {
            @Override
            public void onAdDismissed() {
                Log.i("RSplashActivity", "onAdDismissed");
                jumpWhenCanClick(); // 跳转至您的应用主界面
            }

            @Override
            public void onAdFailed(String arg0) {
                Log.i("RSplashActivity", "onAdFailed");
                jump();
            }

            @Override
            public void onAdPresent() {
                Log.i("RSplashActivity", "onAdPresent");
            }

            @Override
            public void onAdClick() {
                Log.i("RSplashActivity", "onAdClick");
                // 设置开屏可接受点击时，该回调可用
            }
        };
        String adPlaceId = "5531993"; // 重要：请填上您的广告位ID，代码位错误会导致无法请求到广告
        new SplashAd(this, adsParent, listener, adPlaceId, true);
//
        timed = (TextView) findViewById(R.id.splash_time);
//        final DisplayMetrics dp = Util.getDisplayMetrics(this);
//        final int phone_width = dp.widthPixels;
//        final int phone_height = dp.heightPixels;
//        final float phone_rate = phone_height / (float) phone_width;
        mainHandler.postDelayed(closeRunnable, 1000);
    }
    /**
     * 当设置开屏可点击时，需要等待跳转页面关闭后，再切换至您的主窗口。故此时需要增加canJumpImmediately判断。 另外，点击开屏还需要在onResume中调用jumpWhenCanClick接口。
     */
    public boolean canJumpImmediately = false;

    private void jumpWhenCanClick() {
        Log.d("test", "this.hasWindowFocus():" + this.hasWindowFocus());
        if (canJumpImmediately) {
            this.startActivity(new Intent(SplashActivity.this, QiangHongBaoActivity.class));
            splashOK = true;
            this.finish();
        } else {
            canJumpImmediately = true;
        }

    }

    /**
     * 不可点击的开屏，使用该jump方法，而不是用jumpWhenCanClick
     */
    private void jump() {
        this.startActivity(new Intent(SplashActivity.this, QiangHongBaoActivity.class));
        splashOK = true;
        this.finish();
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (canJumpImmediately) {
            jumpWhenCanClick();
        }
        canJumpImmediately = true;
    }
}
